package com.ruicar.afs.cloud.apply.pre.loan.mq.receiver;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruicar.afs.cloud.bizcommon.rules.entity.*;
import com.ruicar.afs.cloud.bizcommon.rules.mapper.*;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleOperateHisService;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentManagement;
import com.ruicar.afs.cloud.image.entity.ComAttachmentRule;
import com.ruicar.afs.cloud.image.service.ComAttaManageService;
import com.ruicar.afs.cloud.image.service.ComAttachmentRuleService;
import com.ruicar.afs.cloud.image.vo.AttachmentDataSendVo;
import com.ruicar.afs.cloud.image.vo.AttachmentRuleVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Ice
 * @date 2020/8/27 15:25
 */
@Slf4j
@Component
@AllArgsConstructor
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class ApplyChannelFilterParamsRecipient implements AfsMqBizProcessor<AttachmentDataSendVo> {
    private final ComAttaManageService comAttaManageService;
    private final AfsRuleInfoService ruleInfoService;
    private final AfsRuleInputMappingMapper mappingMapper;
    private final AfsRuleItemMapper ruleItemMapper;
    private final AfsRuleOperateHisService ruleOperateHisService;
    private final AfsRuleTransExpressMapper ruleTransExpressMapper;
    private final AfsRuleExpressMapper ruleExpressMapper;
    private final AfsRuleExpressAtomMapper ruleExpressAtomMapper;
    private final ComAttachmentRuleService comAttachmentRuleService;
    @Override
    public boolean processMessage(AttachmentDataSendVo entity) throws Exception {

        if (ObjectUtil.isNotNull(entity)) {
            log.info("已成功接收到影像件MQ报文，解析报文中... ......(*￣０￣)ノ");
            this.getFileterParams(entity);
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.CHANNEL_CHANNEL_APPLY_POS_NEW_ATTACHMENT_APPLY;
    }

    @Override
    public boolean reQueueOnFail() {
        return false;
    }


    /**
     * 解析MQ报文
     *
     * @param entity
     */
    public void getFileterParams(AttachmentDataSendVo entity) {
        ComAttachmentManagement management = entity.getManagement();
        List<ComAttachmentRule> comAttachmentRuleList = entity.getRuleList();
        List<AttachmentRuleVo> ruleVoList = entity.getRuleVoList();
        if (ObjectUtil.isNotNull(management)) {
            ComAttachmentManagement comAttachmentManagement = comAttaManageService.getById(management.getId());
            if (ObjectUtil.isNotNull(comAttachmentManagement)) {
                comAttaManageService.updateById(management);
            } else {
                comAttaManageService.save(management);
            }
        } else {
            throw new AfsBaseException("ComAttachmentManagement为空");
        }
        if (CollectionUtil.isNotEmpty(comAttachmentRuleList)) {
            for (ComAttachmentRule comAttachmentRule : comAttachmentRuleList) {
                if (ObjectUtil.isNotNull(comAttachmentRule)) {
                    ComAttachmentRule serviceById = this.comAttachmentRuleService.getById(comAttachmentRule.getId());
                    if (ObjectUtil.isNotNull(serviceById)) {
                        this.comAttachmentRuleService.updateById(comAttachmentRule);
                    } else {
                        this.comAttachmentRuleService.save(comAttachmentRule);
                    }
                }
            }
        }
        if (CollectionUtil.isNotEmpty(ruleVoList)) {
            for (AttachmentRuleVo attachmentRuleVo : ruleVoList) {
                if (ObjectUtil.isNotNull(attachmentRuleVo)) {
                    List<List<AfsRuleExpress>> afsRuleExpressList = attachmentRuleVo.getRuleExpress();
                    if (CollectionUtil.isNotEmpty(afsRuleExpressList)) {
                        for (List<AfsRuleExpress> ruleExpressList : afsRuleExpressList) {
                            ruleExpressList.stream().forEach(afsRuleExpress -> {
                                if (ObjectUtil.isNotNull(afsRuleExpress)) {
                                    AfsRuleExpress ruleExpress = this.ruleExpressMapper.selectById(afsRuleExpress.getId());
                                    if (ObjectUtil.isNotNull(ruleExpress)) {
                                        this.ruleExpressMapper.updateById(afsRuleExpress);
                                    } else {
                                        this.ruleExpressMapper.insert(afsRuleExpress);
                                    }
                                }
                            });
                        }
                    }
                    List<List<AfsRuleExpressAtom>> afsRuleExpressAtomList = attachmentRuleVo.getRuleExpressAtom();
                    if (CollectionUtil.isNotEmpty(afsRuleExpressAtomList)) {
                        for (List<AfsRuleExpressAtom> ruleExpressAtomList : afsRuleExpressAtomList) {
                            ruleExpressAtomList.stream().forEach(afsRuleExpressAtom -> {
                                if (ObjectUtil.isNotNull(afsRuleExpressAtom)) {
                                    AfsRuleExpressAtom ruleExpressAtom = this.ruleExpressAtomMapper.selectById(afsRuleExpressAtom.getId());
                                    if (ObjectUtil.isNotNull(ruleExpressAtom)) {
                                        this.ruleExpressAtomMapper.updateById(afsRuleExpressAtom);
                                    } else {
                                        this.ruleExpressAtomMapper.insert(afsRuleExpressAtom);
                                    }

                                }
                            });
                        }
                    }
                    List<AfsRuleInfo> afsRuleInfoList = attachmentRuleVo.getRuleInfo();
                    if (CollectionUtil.isNotEmpty(afsRuleInfoList)) {
                        afsRuleInfoList.stream().forEach(afsRuleInfo -> {
                            if (ObjectUtil.isNotNull(afsRuleInfo)) {
                                AfsRuleInfo ruleInfo = this.ruleInfoService.getById(afsRuleInfo.getId());
                                if (ObjectUtil.isNotNull(ruleInfo)) {
                                    this.ruleInfoService.updateById(afsRuleInfo);
                                } else {
                                    this.ruleInfoService.save(afsRuleInfo);
                                }
                            }

                        });
                    }
                    List<List<AfsRuleInputMapping>> afsRuleInputMappingList = attachmentRuleVo.getRuleInputMapping();
                    if (CollectionUtil.isNotEmpty(afsRuleInputMappingList)) {
                        for (List<AfsRuleInputMapping> ruleInputMappingList : afsRuleInputMappingList) {
                            ruleInputMappingList.stream().forEach(afsRuleInputMapping -> {
                                if (ObjectUtil.isNotNull(afsRuleInputMapping)) {
                                    AfsRuleInputMapping ruleInputMapping = this.mappingMapper.selectById(afsRuleInputMapping.getId());
                                    if (ObjectUtil.isNotNull(ruleInputMapping)) {
                                        this.mappingMapper.updateById(afsRuleInputMapping);
                                    } else {
                                        this.mappingMapper.insert(afsRuleInputMapping);
                                    }
                                }
                            });
                        }
                    }
                    List<List<AfsRuleItem>> afsRuleItemList = attachmentRuleVo.getRuleItem();
                    if (CollectionUtil.isNotEmpty(afsRuleItemList)) {
                        for (List<AfsRuleItem> ruleItemList : afsRuleItemList) {
                            ruleItemList.stream().forEach(afsRuleItem -> {
                                if (ObjectUtil.isNotNull(afsRuleItem)) {
                                    AfsRuleItem ruleItem = this.ruleItemMapper.selectById(afsRuleItem.getId());
                                    if (ObjectUtil.isNotNull(ruleItem)) {
                                        this.ruleItemMapper.updateById(afsRuleItem);
                                    } else {
                                        this.ruleItemMapper.insert(afsRuleItem);
                                    }
                                }
                            });
                        }
                    }
                    List<List<AfsRuleOperateHis>> ruleOperateHisList = attachmentRuleVo.getRuleOperateHis();
                    if (CollectionUtil.isNotEmpty(ruleOperateHisList)) {
                        for (List<AfsRuleOperateHis> afsRuleOperateHisList : ruleOperateHisList) {
                            afsRuleOperateHisList.stream().forEach(afsRuleOperateHis -> {
                                if (ObjectUtil.isNotNull(afsRuleOperateHis)) {
                                    AfsRuleOperateHis ruleOperateHis = this.ruleOperateHisService.getById(afsRuleOperateHis.getId());
                                    if (ObjectUtil.isNotNull(ruleOperateHis)) {
                                        this.ruleOperateHisService.updateById(afsRuleOperateHis);
                                    } else {
                                        log.info("数据库不存在该数据，直接插入");
                                        this.ruleOperateHisService.save(afsRuleOperateHis);
                                    }
                                }
                            });

                        }
                    }
                    List<List<AfsRuleTransExpress>> transExpressList = attachmentRuleVo.getRuleTransExpress();
                    if (CollectionUtil.isNotEmpty(transExpressList)) {
                        for (List<AfsRuleTransExpress> afsRuleTransExpressList : transExpressList) {
                            afsRuleTransExpressList.stream().forEach(afsRuleTransExpress -> {
                                if (ObjectUtil.isNotNull(afsRuleTransExpress)) {
                                    AfsRuleTransExpress ruleTransExpress = this.ruleTransExpressMapper.selectById(afsRuleTransExpress.getId());
                                    if (ObjectUtil.isNotNull(ruleTransExpress)) {
                                        this.ruleTransExpressMapper.updateById(afsRuleTransExpress);
                                    } else {
                                        this.ruleTransExpressMapper.insert(afsRuleTransExpress);
                                    }
                                }
                            });
                        }
                    }
                }
            }
            log.info("规则信息落库完成");
        } else {
            log.error("附件规则信息为空");
        }
    }
}
